package com.yfp.client.controller;

import com.yfp.client.annotation.AnonymousAccess;
import com.yfp.client.domain.MarActivity;
import com.yfp.client.domain.MarCoupon;
import com.yfp.client.domain.MarCouponPack;
import com.yfp.client.domain.Marketing;
import com.yfp.client.domain.vo.ActivityConfigVO;
import com.yfp.client.domain.vo.ClientActivityRewardsRankVO;
import com.yfp.client.domain.vo.ClientActivityVO;
import com.yfp.client.domain.vo.CountClientActivityVO;
import com.yfp.client.enums.ActivityStatusEnum;
import com.yfp.client.enums.ActivityTypeEnum;
import com.yfp.client.enums.MarketingTypeEnum;
import com.yfp.client.service.ClientActivityService;
import com.yfp.client.service.MarActivityService;
import com.yfp.client.service.MarCouponPackService;
import com.yfp.client.service.MarCouponService;
import com.yfp.client.util.Response;
import com.yfp.client.util.ResponseEnum;
import com.yfp.client.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description: 客户活动
 * 
 * @CreateDate: 2024/7/19 13:50
 * @UpdateUser: 
 * @UpdateDate: 2024/7/19 13:50
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@RestController
@RequestMapping("/client/clientActivity")
@Tag(name = "客户活动")
public class ClientActivityController {
    @Autowired
    private ClientActivityService clientActivityService;
    @Autowired
    private MarCouponService marCouponService;
    @Autowired
    private MarActivityService marActivityService;
    @Autowired
    private MarCouponPackService marCouponPackService;

    @Operation(summary = "邀请有礼-统计邀请人数和奖励")
    @GetMapping("/countClientActivity")
    public Response<CountClientActivityVO> countClientActivity(Long marketingId) {
        CountClientActivityVO countClientActivityVO = clientActivityService.countClientActivityByMarketingIdAndClientId(marketingId, SecurityUtils.getClientId());
        return Response.success(countClientActivityVO);
    }

    @Operation(summary = "邀请有礼-被邀请人列表")
    @GetMapping("/getActivityClient")
    public Response<List<ClientActivityVO>> getActivityClient(Long marketingId) {
        List<ClientActivityVO> clientActivityVOS = clientActivityService.findByMarketingIdAndClientId(marketingId, SecurityUtils.getClientId());
        return Response.success(clientActivityVOS);
    }

    @Operation(summary = "获取活动配置信息")
    @GetMapping("/getByMarketingId")
    @AnonymousAccess
    @ApiResponse(responseCode = "200", description = "查询成功", content = @Content(schema = @Schema(implementation = ActivityConfigVO.class)))
    public Response getByMarketingId(Long marketingId) {
        ActivityConfigVO activityConfigVO = new ActivityConfigVO();
        Marketing marketing = clientActivityService.findMarketingById(marketingId);
        if (null == marketing) {
            return Response.failuer("无该活动");
        }
        if (marketing.getType() == MarketingTypeEnum.COUPON.getType()) {
            MarCoupon marCoupon = clientActivityService.findMarCouponById(marketing.getMarCouponId());
            BeanUtils.copyProperties(marCoupon, activityConfigVO);
            activityConfigVO.setMarCoupon(marCoupon);
        } else if (marketing.getType() == MarketingTypeEnum.ACTIVITY.getType()) {
            MarActivity marActivity = clientActivityService.findMarActivityById(marketing.getMarActivityId());
            BeanUtils.copyProperties(marActivity, activityConfigVO);
        } else if (marketing.getType() == MarketingTypeEnum.COUPONPACK.getType()) {
            MarCouponPack marCouponPack = marCouponPackService.findByMarketingId(marketing.getId());
            BeanUtils.copyProperties(marCouponPack, activityConfigVO);
        }
        return Response.success(activityConfigVO);
    }

    @Operation(summary = "获取邀请有礼配置信息")
    @GetMapping("/getInvitation")
    @AnonymousAccess
    public Response<ActivityConfigVO> getInvitation() {
        ActivityConfigVO activityConfigVO = new ActivityConfigVO();
        String cityName = SecurityUtils.getCityName();

        MarActivity marActivity = clientActivityService.findValidByType(ActivityTypeEnum.NEW_RECRUITMENT_ACTIVITIES.getType(),cityName);


        if (null != marActivity && marActivity.getStatus() == ActivityStatusEnum.NORMAL.getStatus()) {
            BeanUtils.copyProperties(marActivity, activityConfigVO);
        }else if(null != marActivity && marActivity.getStatus() == ActivityStatusEnum.NOT_STARTED.getStatus()) {
            return Response.result(ResponseEnum.NOT_STARTED_ACTIVITY);
        } else if (null != marActivity && (marActivity.getStatus() == ActivityStatusEnum.EXPIRED.getStatus() || marActivity.getStatus() == ActivityStatusEnum.INVALID.getStatus())){
            return Response.result(ResponseEnum.SHIXIAO_ACTIVITY);
        } else if (null == marActivity){
            return Response.result(ResponseEnum.NOT_ACTIVITY);
        }
        return Response.success(activityConfigVO);
    }


    @Operation(summary = "邀请有礼-排行榜")
    @GetMapping("/rewards/rank")
    public Response<List<ClientActivityRewardsRankVO>> getRewardsRank(Long marketingId) {
        List<ClientActivityRewardsRankVO> list = clientActivityService.getRewardsRank(marketingId);
        return Response.success(list);
    }
}
